package pkg

import (
	"common/initialization"
	"fmt"
	"github.com/smartwalle/alipay/v3"
)

type Pay interface {
	ALiPay(Subject, OutTradeNo, TotalAmount string) (string, error)
}
type AliPayConf struct {
	privateKey string
	appId      string
	NotifyURL  string
	ReturnURL  string
}

func NewAliPay() *AliPayConf {
	cf := initialization.Config.Alipay
	return &AliPayConf{
		privateKey: cf.PrivateKey,
		appId:      cf.AppId,
		NotifyURL:  cf.NotifyURL,
		ReturnURL:  cf.ReturnURL,
	}

}
func (a *AliPayConf) ALiPay(Subject, OutTradeNo, TotalAmount string) (string, error) {
	var privateKey = a.privateKey // 必须，上一步中使用 RSA签名验签工具 生成的私钥
	client, err := alipay.New(a.appId, privateKey, false)
	if err != nil {
		return "", err
	}
	var p = alipay.TradeWapPay{}
	p.NotifyURL = a.NotifyURL
	p.ReturnURL = a.ReturnURL
	fmt.Println(p.NotifyURL, p.ReturnURL, 8989)
	p.Subject = Subject         //"标题"
	p.OutTradeNo = OutTradeNo   //"传递一个唯一单号"
	p.TotalAmount = TotalAmount //"10.00"
	p.ProductCode = "QUICK_WAP_WAY"

	url, err := client.TradeWapPay(p)
	if err != nil {
		return "", err
	}
	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	return payURL, nil
}
